/**
 * Created by zhubaojun on 16/6/16.
 */
var express = require('express');

var app = express();
//设置视图引擎
var handlebars = require('express3-handlebars').create({defaultLayout:'main'});
app.engine('handlebars',handlebars.engine);
app.set('view engine','handlebars');


var textPlainStr = 'text/plain';

var fortune = require('./lib/fortune.js');
var fortuneCookies = [
    "Conquer your fears or they will conquer you.",
    "Rivers need springs.",
    "Do not fear what you don't know.",
    "You will have a pleasant surprise.",
    "Whenever possible, keep it simple.",
];

app.set('port',process.env.PORT||3000);

//static
app.use(express.static(__dirname + '/public'));








//定制404页面
//home page
app.get('/', function (req, res) {
    //res.type(textPlainStr);
    //res.send('Meadowlark Travel');

    res.render('home');
});

app.get('/about', function (req, res) {
    //res.type(textPlainStr);
    //res.send('About Meadowlark Travel');


    //var randomInteger =Math.random() * fortuneCookies.length;
    //console.log(Math.floor(randomInteger));
    //var randomFortune = fortuneCookies[Math.floor(randomInteger)];
    //console.log(randomFortune);
    //res.render('about',{fortune:randomFortune});

    console.log(fortune.getFortune());
    res.render('about',{fortune:fortune.getFortune()});


});

app.use(function (req, res) {
    //res.type(textPlainStr);
    //res.status(404);
    //res.send('404 - Not Found');
    res.status(404);
    res.render('404');
});


app.use(function (err, req, res, next) {
    console.error(err.stack);
    //res.type(textPlainStr);
    //res.status(500);
    //res.send('500 - Server Error');

    res.status(500);
    res.render('500');
});





app.listen(app.get('port'), function () {
    console.log('local host at '+app.get('port'));
});